package weaver.interfaces.ybcn.duss.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.ybcn.duss.utils.T100Util;
import weaver.soa.workflow.request.RequestInfo;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2024-04-15
 * @Description: 销售合同变更流程调用cwssp013生成销售合同变更单接口
 * @ModifyLog: OA-T100，OA将销售合同信息传给T100；
 **/
public class WeaXshtbglcAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        String requestname = requestInfo.getRequestManager().getRequestname();
        writeLog(">>>>>WeaXshtbglcAction>>>>>requestid=" + requestid);
        try {
            RecordSet rs = new RecordSet();
            T100Util t100Util = new T100Util();
            rs.execute("select * from " + tablename + " where requestid=?", requestid);
            if (rs.next()) {
                JSONObject payloadJson = new JSONObject();
                JSONObject stddataJson = new JSONObject();
                JSONObject parameterJson = new JSONObject();
                JSONArray masterList = new JSONArray();
                JSONObject masterJson = new JSONObject();
                String mainid = Util.null2String(rs.getString("id"));
                //来源单号	来记录对方的唯一key	请求id
                masterJson.put("source_no", requestid);
                //合同单号	合同类型
                masterJson.put("xmeadocno", Util.null2String(rs.getString("htlxx")));
                //单据日期	为空则默认为 当天	申请日期
                masterJson.put("xmea902", Util.null2String(rs.getString("sqrq")));
                //销售人员	为空则默认为 当前操作用户	申请人
                masterJson.put("xmea002", Util.null2String(rs.getString("sqr")));
                //客户		客户编码	KUNNR	传业务伙伴编号
                masterJson.put("xmea004",Util.null2String(rs.getString("KUNNR")) );
                //币种	需要根据oa单身的币种传递到单头　	币种	bz1	传编码	新增字段，浏览按钮-币种
                masterJson.put("xmdx005", Util.null2String("bz1"));
                //生效日期	合同开始时间
                masterJson.put("xmdx014", Util.null2String(rs.getString("GUEBG")));
                //失效日期		合同有效时间
                masterJson.put("xmdx015", Util.null2String(rs.getString("GUEEN")));
                //备注		标题
                masterJson.put("xmdx030", requestname);

                JSONArray detailList = new JSONArray();
                rs.executeQuery("select * from "+tablename+"_dt2 where mainid=?",mainid);
                int count = 1;
                while (rs.next()){
                    JSONObject detailJson = new JSONObject();
                    String id = Util.null2String(rs.getString("id"));
                    //项次		序号
                    detailJson.put("source_seq",count);
                    //料件编号	产品名称
                    detailJson.put("xmeb002",Util.null2String(rs.getString("MATNR")));
                    //产品特征	产品名称
                    detailJson.put("xmeb003",Util.null2String(rs.getString("cpmc")));
                    //单位		单位	dw	传代码	新增字段，浏览按钮-计量单位
                    detailJson.put("xmeb008",Util.null2String(rs.getString("dw")));
                    //数量		数量
                    detailJson.put("xmeb009",Util.null2String(rs.getString("KWMENG")));
                    //备注		销售备注
                    detailJson.put("xmeb030",Util.null2String(rs.getString("ZVTEXT")));
                    //金属价		金属价
                    detailJson.put("xmebud011",Util.null2String(rs.getString("ZPR1_MENG")));
                    //加工费		加工费
                    detailJson.put("xmebud012",Util.null2String(rs.getString("ZPR2_MENG")));

                    detailList.add(detailJson);
                    count++;
                }
                masterJson.put("detail", detailList);
                masterList.add(masterJson);
                parameterJson.put("master", masterList);
                stddataJson.put("parameter", parameterJson);
                payloadJson.put("std_data", stddataJson);
                writeLog(">>>>>WeaXshtbglcAction>>>>>payloadJson=" + payloadJson);
                JSONObject resultJson = t100Util.ContractChangesCreate(payloadJson, "WeaXshtbglcAction", requestid, src);
                writeLog(">>>>>WeaXshtbglcAction>>>>>resultJson=" + resultJson);
                String message = Util.null2String(resultJson.getString("message"));
                if ("success".equals(message)) {
                    JSONObject dataJson = JSONObject.parseObject(resultJson.getString("data"));
                    JSONObject retPayloadJson = JSONObject.parseObject(dataJson.getString("payload"));
                    JSONObject retStddataJson = JSONObject.parseObject(retPayloadJson.getString("std_data"));
                    JSONObject retParameterJson = JSONObject.parseObject(retStddataJson.getString("parameter"));
                    int failnum = Util.getIntValue(retParameterJson.getString("failnum"), 0);
                    if (failnum > 0) {
                        JSONArray failreturnList = JSONArray.parseArray(retParameterJson.getString("fail_return"));
                        String errorMsg = failreturnList.getJSONObject(0).getString("error_msg");
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent("调用T100系统接口返回:" + errorMsg);
                        return Action.FAILURE_AND_CONTINUE;
                    } else {
                        JSONArray successreturnList = JSONArray.parseArray(retParameterJson.getString("success_return"));
                        String successMsg = successreturnList.getJSONObject(0).getString("success_msg");
                        rs.execute("update "+tablename+" set PARTNER='"+successMsg+"' where requestid=?",requestid);
                    }
                } else {
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent("调用T100系统接口返回:" + message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaXshtbglcAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}